﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GradebookApp.gradebookMufSk;

namespace GradebookApp
{
    public partial class Form1 : Form
    {
        GradebookService gradebook = new GradebookService();

        public Form1()
        {
            InitializeComponent();
        }

        private void get_btn_Click(object sender, EventArgs e)
        {
            get_btn.Enabled = false;
            info_listbox.Items.Clear();
            try
            {
            if (getClasses_rb.Checked)
            {
                List<@class> classesList = new List<@class>(gradebook.getClasses());
                foreach (@class c in classesList)
                {
                    info_listbox.Items.Add(c.name + " (ID " + c.classId + ")");
                }
            }
            if (getStudentsForClass_rb.Checked)
            {
                int classId;
                if (Int32.TryParse(classId_tb.Text, out classId))
                {
                    student[] studentsArray = gradebook.getStudentsOfClass(classId);
                    if (studentsArray != null)
                    {
                        List<@student> studentsList = new List<@student>(studentsArray);
                        foreach (@student s in studentsList)
                        {
                            info_listbox.Items.Add(s.firstName + " " + s.lastName);
                        }
                    }
                }
            }
            if (getTimetablesOfClass_rb.Checked)
            {
                int classId;
                if (Int32.TryParse(classId2_tb.Text, out classId))
                {
                    timetable[] timetableArray = gradebook.getTimetablesOfClass(classId);
                    if (timetableArray != null)
                    {
                        List<@timetable> timetableList = new List<@timetable>(timetableArray);
                        foreach (@timetable t in timetableList)
                        {
                            String text = "";
                            switch (t.day)
                            {
                                case 1: text += "Pon";
                                    break;
                                case 2: text += "Uto";
                                    break;
                                case 3: text += "Str";
                                    break;
                                case 4: text += "Štv";
                                    break;
                                case 5: text += "Pia";
                                    break;
                            }
                            text += " " + t.time + ". hod.:";
                            text += " " + t.subject.name;
                            text += " s " + t.teacher.firstName + " " + t.teacher.lastName;
                            info_listbox.Items.Add(text);
                        }
                    }
                }
            }
            if (getTeachers_rb.Checked)
            {
                List<@teacher> teachersList = new List<@teacher>(gradebook.getTeachers());
                foreach (@teacher t in teachersList)
                {
                    info_listbox.Items.Add(t.firstName + " " + t.lastName);
                }
            }
            if (getSubstitution_rb.Checked)
            {
                List<@dateSubstitutions> dateSubstitutionsList = new List<@dateSubstitutions>(gradebook.getSubstitutions());
                foreach (@dateSubstitutions dateSubstitution in dateSubstitutionsList)
                {
                    if (dateSubstitution.substitutions != null)
                    {
                        List<@substitutionItem> substitutionsItemsList = new List<@substitutionItem>(dateSubstitution.substitutions);
                        foreach (@substitutionItem item in substitutionsItemsList)
                        {
                            String text = "";
                            text += dateSubstitution.date.ToShortDateString() + " ";
                            text += "chýba " + item.missingTeacher.firstName + " " + item.missingTeacher.lastName + " ";
                            text += "na " + item.lesson + ". hodine";
                            text += ". Trieda " + item.className + " ";
                            text += "bude mať " + item.subject.name + " ";
                            text += "s " + item.substitutingTeacher.firstName + " " + item.substitutingTeacher.lastName + ".";
                            info_listbox.Items.Add(text);
                        }
                    }
                }
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            get_btn.Enabled = true;
        }

        private void getStudentsForClass_rb_CheckedChanged(object sender, EventArgs e)
        {
            classId_tb.Text = "";
            if (getStudentsForClass_rb.Checked)
            {
                classId_tb.Enabled = true;
            }
            else
            {
                classId_tb.Enabled = false;
            }
        }

        private void getTimetablesOfClass_rb_CheckedChanged(object sender, EventArgs e)
        {
            classId2_tb.Text = "";
            if (getTimetablesOfClass_rb.Checked)
            {
                classId2_tb.Enabled = true;
            }
            else
            {
                classId2_tb.Enabled = false;
            }
        }
    }
}